﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace envir2plotwatt.GUI.Configuration
{
    public partial class MonitorMapping : Form
    {
        public GetMonitorListDelegate GetMonitorList { get; set; }
        public GetMeterListDelegate GetMeterList { get; set; }
        public AddMonitorMeterMappingDelegate AddMonitorMapping { get; set; }
        public GetMonitorMeterMappingDelegate GetMonitorMapping { get; set; }
        public DeleteMonitorMeterMappingDelegate DelMonitorMapping { get; set; }

        public MonitorMapping()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddEditMonitorMapping addEditForm = new AddEditMonitorMapping(AddEditMonitorMapping.FormMode.Add);
            addEditForm.GetMeterList = GetMeterList;
            addEditForm.GetMonitorList = GetMonitorList;

            if (addEditForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                AddMonitorMapping(addEditForm.Mapping);
                UpdateMappingGrid();
            }
        }

        private void MonitorMapping_Load(object sender, EventArgs e)
        {
            UpdateMappingGrid();
        }

        private void UpdateMappingGrid()
        {
            dataGridView1.DataSource = null;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = GetMonitorMapping();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            // check if we have a selected item
            if (dataGridView1.SelectedRows.Count == 1)
            {
                MonitorToMeter mm = (MonitorToMeter)dataGridView1.SelectedRows[0].DataBoundItem;

                AddEditMonitorMapping addEditForm = new AddEditMonitorMapping(AddEditMonitorMapping.FormMode.Edit, mm);
                addEditForm.GetMeterList = GetMeterList;
                addEditForm.GetMonitorList = GetMonitorList;

                if (addEditForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    mm.Meter =  addEditForm.Mapping.Meter;
                    mm.Monitor = addEditForm.Mapping.Monitor;

                    UpdateMappingGrid();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            // check if we have a selected item
            if (dataGridView1.SelectedRows.Count == 1)
            {
                MonitorToMeter mm = (MonitorToMeter)dataGridView1.SelectedRows[0].DataBoundItem;
                DelMonitorMapping(mm);
                UpdateMappingGrid();
            }
        }

        private void Finished_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
